package com.huluobo.a2308douyindemo.adapter

import android.animation.ObjectAnimator
import android.view.View
import android.view.animation.LinearInterpolator
import android.widget.ImageView
import com.chad.library.adapter.base.BaseMultiItemQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.huluobo.a2308douyindemo.R
import com.huluobo.a2308douyindemo.bean.VideoBean
import com.huluobo.a2308douyindemo.utils.ImageUtils
import com.shuyu.gsyvideoplayer.video.StandardGSYVideoPlayer

/**
 *  Created by LC on 2025/10/28.
 */

//布局的类型,用于区分布局
object ItemType {
    const val TYPE_VIDEO = 0
    const val TYPE_FULL = 1
    const val TYPE_LIVE = 2
}

class MultiVideoAdapter : BaseMultiItemQuickAdapter<VideoBean, BaseViewHolder>() {

    init {
        addItemType(ItemType.TYPE_VIDEO, R.layout.item_videos)
        addItemType(ItemType.TYPE_FULL, R.layout.item_videos_full)
        addItemType(ItemType.TYPE_LIVE, R.layout.item_videos_live)
    }

    override fun convert(holder: BaseViewHolder, item: VideoBean) {
        //获取播放器
        val player = holder.getView<StandardGSYVideoPlayer>(R.id.item_videos_player)
        //设置返回按键消失
        player.backButton.visibility = View.GONE
        //设置播放地址,1.播放地址,2.边缓存边播放,3.视频标题
        player.setUp(item.videopath, true, "")

        //左下角赋值
        holder.setText(R.id.item_videos_pao_tv, item.description)
        holder.setText(R.id.item_videos_time_tv, item.ctime)
        holder.setText(R.id.item_videos_desc_tv, item.title)
        holder.setText(R.id.item_videos_name_tv, item.name)

        when (holder.itemViewType) {
            ItemType.TYPE_VIDEO, ItemType.TYPE_FULL -> {
                //赋值右下角布局,由于Live的布局没有右下角,所以需要作出区分
                //加载头像
                ImageUtils.loadCircleImage(context, item.avatar_url, holder.getView(R.id.item_videos_header_iv))

                //如果该字段为true,就证明已点赞
                if (item.isLiked) {
                    holder.setImageResource(R.id.item_videos_like_iv, R.drawable.ic_xihuanred)
                } else {
                    holder.setImageResource(R.id.item_videos_like_iv, R.drawable.ic_xihuan)
                }

                if (item.isCollect) {
                    holder.setImageResource(R.id.item_videos_collect_iv, R.drawable.ic_shoucangyellow)
                } else {
                    holder.setImageResource(R.id.item_videos_collect_iv, R.drawable.ic_shoucang)
                }

                //获取右下角音乐播放器
                val musicPlayer = holder.getView<ImageView>(R.id.item_videos_music_player_iv)
                ImageUtils.loadCircleImage(context, item.videomainimag, musicPlayer)

                val animator = ObjectAnimator.ofFloat(musicPlayer, "rotation", 0f, 360f)
                animator.duration = 3000
                animator.repeatCount = -1
                animator.interpolator = LinearInterpolator()
                animator.start()
            }

            else -> {}
        }
    }
}